import heapq
n=int(input())
a=list(map(int,input().split()))

# 堆获取最小元素，添加元素
# 将a转换为堆
heapq.heapify(a)
ans=0
while len(a)>=2:
    # 取出两个最小元素，合并即可
    x=heapq.heappop(a)
    y=heapq.heappop(a)
    heapq.heappush(a,x+y)
    ans+=x+y
print(ans)
